<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>个人信息</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .container {
            margin-top: -200px;
            background-color: #ffffff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 400px;
            text-align: center;
        }
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            transition: border-color 0.3s;
        }
        .form-group input:focus {
            border-color: #007bff;
            outline: none;
        }
        .submit-btn {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .submit-btn:hover {
            background-color: #0056b3;
        }
        .back-btn {
            position: absolute;
            top: 20px;
            left: 20px;
            background: transparent;
            border: none;
            font-size: 24px;
            color: #007bff;
            cursor: pointer;
            transition: color 0.3s;
        }
        .back-btn:hover {
            color: #0056b3;
        }
        #deleteBtn {
            position: absolute;
            background: #e45050;
            margin-left: 100px;
            width: auto;
            height: auto;
            margin-top: -106px;
        }
        #deleteBtn:hover {
            background: #c43d3d;
        }
    </style>
</head>
<body>
<!-- 返回按钮 -->
<button class="back-btn" onclick="window.history.back()">←</button>

<div class="container">
    <h1>个人信息</h1>
    <button type="button" id="deleteBtn" class="submit-btn">注销当前账户</button>

    <p style="color: red;"> 修改成功后需要重新登录！</p>
    <form id="personalInfoForm" action="PersonalInfo" method="post">
        <div class="form-group">
            <label for="uId">学号:</label>
            <input type="text" id="uId" name="uId" value="${USER.uId}" readonly>
        </div>
        <div class="form-group">
            <label for="name">姓名:</label>
            <input type="text" id="name" name="name" value="${USER.name}" readonly>
        </div>
        <div class="form-group">
            <label for="classname">班级:</label>
            <input type="text" id="classname" name="classname" value="${USER.classname}" readonly>
        </div>
        <div class="form-group">
            <label for="concat">联系方式:</label>
            <input type="text" id="concat" name="concat" value="${USER.concat}" readonly>
        </div>
        <div class="form-group">
            <label for="password">密码:</label>
            <input type="text" id="password" name="password" value="${USER.password}" readonly>
        </div>
        <button type="button" id="editBtn" class="submit-btn">修改信息</button>
        <button type="submit" id="saveBtn" class="submit-btn" style="display: none;">保存</button>
    </form>
</div>

<script>
    // 获取页面元素
    const editBtn = document.getElementById('editBtn');
    const saveBtn = document.getElementById('saveBtn');
    const uIdentityInput = document.getElementById('uId');
    const nameInput = document.getElementById('name');
    const classnameInput = document.getElementById('classname');
    const contactInput = document.getElementById('concat');
    const passwordInput = document.getElementById('password');
    const deleteBtn = document.getElementById('deleteBtn');
    const uIdInput = document.getElementById('uId');

    // 点击“修改信息”按钮
    editBtn.addEventListener('click', () => {
        // 将输入框设置为可编辑
        nameInput.readOnly = false;
        passwordInput.readOnly = false;
        contactInput.readOnly = false;
        classnameInput.readOnly = false;

        // 显示“保存”按钮，隐藏“修改信息”按钮
        saveBtn.style.display = 'block';
        editBtn.style.display = 'none';
    });

    // 点击“保存”按钮
    saveBtn.addEventListener('click', () => {
        // 提交表单
        document.getElementById('personalInfoForm').submit();
    });

    // 点击“注销当前账户”按钮
    deleteBtn.addEventListener('click', () => {
        if (confirm('您确定要注销当前账户吗？')) {
            fetch('deleteAccount', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
            }).then(response => {
                if (response) {
                    alert('账户已注销，正在重定向到登录页面...');
                    window.location.href = 'index.jsp'; // 账户注销成功后重定向到登录页面
                } else {
                    alert('注销账户失败，请稍后重试。');
                }
            }).catch(error => {
                console.error('出错了：', error);
                alert('发生错误，请稍后重试。');
            });
        }
    });
</script>
</body>
</html>
